package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}


type ApiResponse struct {
     Result interface{} `json:"result"`
}

type ApiResponseSign struct {
	Code      int         `json:"code"`
	Message   string      `json:"message"`
	Appkey    string      `json:"appkey"`
	Body      interface{} `json:"body"`
	Timestamp string      `json:"timestamp"`
	Method    string      `json:"method"`
}

const (
	ERROR        = 400
	SUCCESS      = 200
	REMOTE_LOGIN = 402
	INVALIDATION = 401
	BearerError  = 1001
	SignError    = 1003
)

func Result(code int, data interface{}, msg string, c *gin.Context) {
	// 开始时间
	c.JSON(http.StatusOK, Response{
		code,
		data,
		msg,
	})
}

func ApiResult(code int, msg string, header interface{}, body interface{}, c *gin.Context) {

    result := ApiResponse{struct {
        Code    int `json:"code"`
        Message string `json:"message"`
        Header  interface{} `json:"header"`
        Body    interface{} `json:"body"`
    }{Code:code , Message:msg , Header:header , Body:body }}

	c.JSON(http.StatusOK, result)
}

func ApiResultSign(code int, msg string, appkey string, body interface{}, timestamp string, method string, c *gin.Context) {

    result := ApiResponse{struct {
        Code    int `json:"code"`
        Message string `json:"message"`
        Appkey  string `json:"header"`
        Body    interface{} `json:"body"`
        Timestamp string `json:"timestamp"`
        Method string `json:"method"`
    }{
        Code:      code,
        Message:   msg,
        Appkey:    appkey,
        Body:      body,
        Timestamp: timestamp,
        Method:    method,}}

    c.JSON(http.StatusOK, result)
}

func Ok(c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
}

func OkWithMessage(message string, c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, message, c)
}

func OkWithData(data interface{}, c *gin.Context) {
	Result(SUCCESS, data, "操作成功", c)
}

func OkDetailed(data interface{}, message string, c *gin.Context) {
	Result(SUCCESS, data, message, c)
}

func Fail(c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, "操作失败", c)
}

func FailWithMessage(message string, c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, message, c)
}

func FailWithDetailed(code int, data interface{}, message string, c *gin.Context) {
	Result(code, data, message, c)
}
